package com.ml.loan.common.exception;

import java.text.MessageFormat;

/**
 * 自定义异常
 *
 * @author Mickey
 * @date 2022/2/15 18:07
 */
public class BusinessException extends RuntimeException {

    /**
     * 错误码
     **/
    private Integer code;

    /**
     * 是否记录入库
     **/
    private boolean saveState = true;

    public BusinessException(String message) {
        super(message);
    }

    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    public BusinessException(String message, boolean saveState) {
        super(message);
        this.saveState = saveState;
    }

    public BusinessException(Integer code, String message, boolean saveState) {
        super(message);
        this.code = code;
        this.saveState = saveState;
    }

    public BusinessException(Integer code, String message, Object... args) {
        super(MessageFormat.format(message, args));
        this.code = code;
    }

    public Integer getCode() {
        if (code == null) {
            code = 500;
        }
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public boolean isSaveState() {
        return saveState;
    }

    public void setSaveState(boolean saveState) {
        this.saveState = saveState;
    }

    @Override
    public String getMessage() {
        String message = super.getMessage();
        if (message == null) {
            message = "Sorry，服务器开小差啦~";
        }
        return message;
    }
}
